package com.cn.goodsadmin.service;

import com.cn.goodsadmin.common.PageQueryResult;
import com.cn.goodsadmin.common.observes.MessageConfig;
import com.cn.goodsadmin.common.observes.MessageManager;
import com.cn.goodsadmin.dao.DestDTO;
import com.cn.goodsadmin.observer.UpdateOrderStatusMessage;
import com.cn.goodsadmin.po.*;
import com.cn.goodsadmin.util.MoneyUtil;
import com.cn.goodsadmin.util.TimeUtil;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("orderService")
public class OrderService extends BaseService {

    public PageQueryResult<DestDTO> getAllOrderInfo(int pageindex, int pagesize, int appid, int state) throws Exception {
        return orderDao.getOrderInfoPage(null, pageindex, pagesize, appid,state);
    }

    public OrderStatistics getAllOrderStaticeByTime(int day, int status, int appid) throws Exception {
        OrderStatistics orderStatistics = new OrderStatistics();
        List<OrderInfo> orderInfos = orderDao.getAllOrderInfoByTime(day, status, appid);
        List<Userbuylog> userbuylogs = userBuyLogDao.findAllBuyLog(day, null, appid);
        double totalPrice = 0;
        double realPrice = 0;
        //计算总价格
        for (OrderInfo orderInfo : orderInfos) {
            totalPrice += Double.valueOf(orderInfo.getcTotalPrice());
        }
        for (Userbuylog userbuylog : userbuylogs) {
            String goodsid = userbuylog.getcGoodsid();
            GoodsInfo goodsInfo = goodsInfoDao.findGoodsInfoByid(goodsid);
            String price = goodsInfo.getcPrice();
            realPrice += Double.valueOf(price);
        }
        orderStatistics.setTotalPrice(MoneyUtil.coverTwoMoney(totalPrice));
        orderStatistics.setOrderNumber((long) orderInfos.size());
        orderStatistics.setDiscount(MoneyUtil.coverTwoMoney(totalPrice / realPrice));
        orderStatistics.setCoffeeNumber(userbuylogs.size());
        orderStatistics.setAvPrice(MoneyUtil.coverTwoMoney(totalPrice / userbuylogs.size()));
        return orderStatistics;
    }

    public void  sendOrder(String orderid,int appid,String logisticsid,String logisticsname)throws Exception{
        List<OrderInfo> orderInfos=orderDao.findOrderByOrderId(orderid,null,appid);
        if(orderInfos!=null&&orderInfos.size()>0) {
            OrderInfo orderInfo = orderInfos.get(0);
            int oldstatuts = (int) orderInfo.getcStatus();
            if (oldstatuts == 1) {
                //检查订单号是否存在
                Logistics logistics=logisticsDao.findLogisticsByLid(logisticsid);
                if(logistics==null) {
                    //入库添加物流
                    logistics = new Logistics();
                    logistics.setcLogisticsid(logisticsid);
                    logistics.setcName(logisticsname);
                    logisticsDao.save(logistics);
                }else if(!logisticsname.equals(logistics.getcName())){
                    //更新
                    logistics.setcName(logisticsname);
                    logisticsDao.update(logistics);
                }
                //修改订单
                orderInfo.setcStatus((byte) 2);
                orderInfo.setcLogisticsid(logisticsid);
                orderInfo.setcMtime(TimeUtil.getCurrTimeHm());
                orderDao.update(orderInfo);
                UpdateOrderStatusMessage message = new UpdateOrderStatusMessage(MessageConfig.UPDATEORDERSTATUTS, orderInfo);
                MessageManager.getInstanceMessage().putMessage(message);
            }
        }
    }
}
